﻿using System.Collections;

namespace 享元模式_围棋例子
{
    /// <summary>
    /// 围棋棋子工厂类，充当享元工厂类（单列模式设计）
    /// </summary>
    public sealed class IgoChessmanFactory
    {
        //private static readonly  IgoChessmanFactory Instance = new IgoChessmanFactory();
        private Hashtable ht;//享元池.
        private IgoChessmanFactory()
        {
            ht = new Hashtable();
            IgoChessman black, white;
            black = new BlackIgoChessman();
            ht.Add("b", black);
            white = new WhiteIgoChessman();
            ht.Add("w", white);
        }
        /// <summary>
        /// 返回享元工厂类的唯一实例
        /// </summary>
        /// <returns></returns>
        public static IgoChessmanFactory Default { get; }=new IgoChessmanFactory();
        //public static IgoChessmanFactory GetInstance()
        //{
        //    return Instance;
        //}
        /// <summary>
        /// 通过key获取存储在Hashtable(享元池)中的享元对象
        /// </summary>
        /// <param name="color"></param>
        /// <returns></returns>
        public IgoChessman GetIgoChessman(string color)
        {
            return ht[color] as IgoChessman;
        }
        

    }
}
